-- 容器的使用
Create by fall on 28 Jun 2023 Recently revised in 19 Apr 2024
容器的后缀
在 docker 官网查找镜像的时候,会遇到一些后缀,这些后缀表示使用了不同的系统
发行版本
Buster
buster 是基于 Debian Linux 发行的一个版本,这个版本比较新,支持比较全面,受广大 Debian 爱好者的好评!所以像 PHP、Python之类的语言、应用都会使用这个版本的 Debian 搭建 Docker 基础镜像。
Ubuntu
- 18.04 LTS,代号"Bionic Beaver",简称 "bionic"
- Ubuntu 20.04 LTS ,代号"Focal Fossa",简称 Focal
- Ubuntu 22.04 LTS,代号"Jammy Jellyfish",简称 Jammy
- Ubuntu 24.04 LTS,代号"Noble Numbat",简称 Noble,2024-04-25 发布第一个版本
Debian
- alpine:比较 Debian 操作系统来说 Alpine 更加轻巧,轻量发行版
- Debian 12,代号“bookworm”
- Debian 11,代号“bullseye”
- Debian 10,代号“buster”(2024-06-15 end-of-life)
- Debian 9,代号“stretch”(2020-7-18 end-of-life)
其它有关后缀
- otel:OpenTelemetry 提供了一套 APIs、库和工具,用于收集和处理分布式应用程序的遥测数据。它帮助开发者更容易地观察和监控应用的性能,通过跟踪和指标等数据来检测潜在的问题。
- perl:是一种流行的编程语言,常用于系统管理、文本处理以及各种其他任务。
- nanoserver: Windows Server 2016 的标准版和数据中心版中作为附加安装选项发布,是一种更轻、更快、更稳定、更安全且占用资源更少的完整的 Windows Server 安装替代方案
对于不同的容器,拥有不同的变量,不同的端口,以及工作路径等配置,所以这里会列出常见的容器的使用方式,以便进行参考。
如果执行命令时出现问题,请检查是否使用的是 powershell
ubuntu
# 运行 ubuntu
docker run -it --name go-fly ubuntu bash
# 将当前路径映射到容器的 src 路径下
docker run -it --mount type=bind,src="$(pwd)",target=/src ubuntu bash
node
将当前路径和容器路径绑定,使用 node:18.16.1 版本然后在容器内执行 yarn install 和 yarn run dev
powershell
docker run -dp 127.0.0.1:3000:3000 `
-w /app --mount "type=bind,src=$pwd,target=/app" `
node:18.16.1 `
sh -c "yarn install && yarn run dev"
docker run -dp 127.0.0.1:3000:3000 \
-w /app --mount type=bind,src="$(pwd)",target=/app \
node:18.16.1 \
sh -c "yarn install && yarn run dev"
-w /app
- sets the “working directory” or the current directory that the command will run from--mount type=bind,src="$(pwd)",target=/app
- bind mount the current directory from the host into the/app
directory in the containernode:18.16.1
- 使用的镜像,这个镜像也是 Dockerfile 中的指定的镜像sh -c "yarn install && yarn run dev"
- the command. You’re starting a shell usingsh
and runningyarn install
to install packages and then runningyarn run dev
to start the development server. If you look in thepackage.json
, you’ll see that thedev
script startsnodemon
.
数据库
mysql
创建 network
docker network create todo-app
创建 mysql,并且连接到 network
docker run -d `
--network todo-app --network-alias mysql `
--name todo-sql `
-v todo-app-store:/var/lib/mysql `
-e MYSQL_ROOT_PASSWORD=secret `
-e MYSQL_DATABASE=todoApp `
mysql:8.0
创建应用,绑定本地开发文件,并且连接到 network
docker run -d `
--network todo-app `
--name todo-node `
-v todo-
services:
mysql:
image: mysql:5.7.35
container_name: mysql # 容器名称
environment:
MYSQL_ROOT_PASSWORD: 123456 # 指定用户密码
TZ: Asia/Shanghai
ports:
- 3306:3306 #本地端口号与容器内部端口号
volumes: #指定挂载目录
- /usr/etc/mysql/datadir:/var/lib/mysql
- /usr/etc/mysql/config/my.cnf:/etc/mysql/my.cnf
command:
--max_connections=1000
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--default-authentication-plugin=mysql_native_password
mongo
bash
# 5.0.18
docker run -it --name my-mongo -p 6200:27017 -v my-mongo-data:/data/db -v my-mongo-config:/data/configdb mongo:5.0.18 -e MONGO_INITDB_ROOT_USERNAME=fall -e MONGO_INITDB_ROOT_PASSWORD=password123
如果想要使用集群,在开始的时候就要进行配置 --replSet
使用 prisma 时,就需要通过下面的方式创建容器
docker run -itd --name my-mongo -p 6200:27017 -v my-mongo-data:/data/db -v my-mongo-config:/data/configdb mongo:6.0.10 --replSet replSet1
docker compose
version: '2'
name: outstand-graph
services:
mongo:
image: mongo:7.0.8
restart: always
container_name: mongo-test
command: mongod --replSet rs0
ports:
- 7217:27017
volumes:
- ./mongo-data:/data/db
- ./mongo-config:/data/configdb
redies
services:
redis:
image: redis:4.0.7 # 镜像名称以及版本
restart: always # 重启 docker 后该容器也重启
container_name: redis # 容器名称
ports:
- 6379:6379 # 本地端口号与容器内部端口号
volumes: # 指定挂载目录
- /usr/etc/redis/redis.conf:/usr/local/etc/redis/redis.conf # redis.conf 文件和 data 目录分别映射了主机的 redis.conf 文件和主机的 data 目录
- /usr/etc/redis/data:/data
command:
/bin/bash -c "redis-server /usr/local/etc/redis/redis.conf " #使用 command 可以覆盖容器启动后默认执行的命令。这里启动执行指定的 redis.conf 文件
postgres
拉取 postres
# 获取 15.3 版本的 postgres
docker pull postgres:15.4-alpine
# 运行镜像
docker run --name pgsql-standard --env POSTGRES_PASSWORD=fall_zhang --publish 9600:5432 postgres:16.2-bookworm
# 创建一个专用卷用于数据库存储,实现数据持久性
docker run -it --name db-postgres -e POSTGRES_PASSWORD=mypassword -e POSTGRES_USER=fall -e POSTGRES_DB=pgsql_app -p 9600:5432 -v postgresvol:/var/lib/postgresql/data -d postgres:16.2-bookworm
# 用户信息会被保存在 volume 中,请在第一次运行时设定好,或再次运行前删除 volume
docker exec -it db-postgres bash
# docker-compose.yml
postgres:
image: postgres
container_name: postgres-local
user: fall
environment:
POSTGRES_USER: fall
POSTGRES_PASSWORD: password
POSTGRES_DB: myapp
volumes:
postgresvol:/var/lib/postgresql/data
ports:
- "9600:5432"
nginx
services:
nginx:
image: nginx:1.21.3 # 镜像名称以及版本
restart: always # 重启 docker 后该容器也重启
container_name: nginx # 容器名称
ports:
- 80:80 # 本地端口号与容器内部端口号
- 443:443 # 本地端口号与容器内部端口号
volumes: # 指定挂载目录
- ./usr/etc/nginx/html:/usr/share/nginx/html
- ./usr/etc/nginx/log:/var/log/nginx
- ./usr/etc/nginx/conf:/etc/nginx/
nicolaka/netshoot
集成了很多工具,用于网络问题追踪和解决的工具
docker run -it --network todo-app nicolaka/netshoot
gitlab
docker run -d -p 5020:22 -p 5080:80 -p 5040:443 `
--name my-gitlab `
--hostname gitlab.example.com `
--restart unless-stopped `
--shm-size 256m `
-v gitlab_config:/etc/gitlab `
-v gitlab_logs:/var/log/gitlab `
-v gitlab_data:/var/opt/gitlab `
gitlab/gitlab-ce
# gitlab-ce:16.11.10-ce.0 ce Community Edition
# --hostname 匹配你访问 gitlab 的域名
进入到容器中,使用该命令获取 root 用户的密码(密码会在第一次配置重启 24 小时后自动删除)
docker exec -it my-gitlab grep 'Password:' /etc/gitlab/initial_root_password
# seRWjNOwSGZdqi9eheOQAqNXOqGzHQ5kVG1hnrc4PUI=
参考文章
作者 | 文章名称 |
---|---|
杨高超 | 通过 docker 搭建自用的 gitlab 服务 |
官方文档 | |